﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Interface;
using App.Model;
using App.MVC.App_Start;
using Newtonsoft.Json;

namespace App.MVC.Controllers
{
    /// <summary>
    /// 考核范围设置
    /// </summary>
    public class KaoHeBanWeiController : BaseController
    {
        public IJiaoZhiGong _IJiaoZhiGong { set; get; }
        public IMoBan _IMoBan { set; get; }
        public IKeShi _IKeShi { set; get; }
        // GET: KaoHeBanWei
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 按科室设置
        /// </summary>
        /// <returns></returns>
        public ActionResult GongZuoJiHua()
        {
            return View();
        }
        /// <summary>
        /// 按人员设置
        /// </summary>
        /// <returns></returns>
        public ActionResult GongZuoJiHuaRenYuan()
        {
            return View();
        }
        public ActionResult JiXiao()
        {
            return View();
        }
        /// <summary>
        /// 获取教职工列表
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="ksid"></param>
        /// <param name="zwid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public JsonResult GetJiaoZhiGongs(string keyword, int ksid, int zwid, int pageIndex, int pageSize)
        {
            string str = _IJiaoZhiGong.GetEmployees(keyword, ksid, zwid, pageIndex, pageSize);
            JiaoZhiGongList tt = new JiaoZhiGongList();

            if (!string.IsNullOrWhiteSpace(str))
            {
                tt = JsonConvert.DeserializeObject<JiaoZhiGongList>(str);
            }

            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取工作计划集
        /// </summary>
        /// <returns></returns>
        public JsonResult GetGZJHs()
        {
            var str = _IMoBan.GetGZJHList();
            List<GongZuoJiHua> tt = JsonConvert.DeserializeObject<List<GongZuoJiHua>>(str);
            //foreach (var item in tt)
            //{
            //    foreach (var item2 in item.GongZuoJiHuaNeiRongs)
            //    {
            //        var keshistr = _IMoBan.GetMoBanKaoHeFanWei(item2.nrid,SiteInfo.GetUserName());
            //        List<KeShi> fw = JsonConvert.DeserializeObject<List<KeShi>>(keshistr);
            //        item2.KaoHeFanWeis = fw;
            //    }
            //}
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetJXs()
        {
            var str = _IMoBan.GetJXList();
            List<GongZuoJiHua> tt = JsonConvert.DeserializeObject<List<GongZuoJiHua>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetGZJH(int mbid,string username)
        {
            string str = _IMoBan.GetGZJHInfo(mbid);
            GongZuoJiHua tt = JsonConvert.DeserializeObject<GongZuoJiHua>(str);
            foreach (var item in tt.GongZuoJiHuaNeiRongs)
            {
                var keshistr = _IMoBan.GetMoBanKaoHeFanWei(item.nrid, username);
                if (!string.IsNullOrWhiteSpace(keshistr))
                {
                    List<KeShi> fw = JsonConvert.DeserializeObject<List<KeShi>>(keshistr);
                    item.KaoHeFanWeis = fw;
                }
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetJX(int mbid,string username)
        {
            string str = _IMoBan.GetJXInfo(mbid);
            JiXiao tt = JsonConvert.DeserializeObject<JiXiao>(str);
            foreach (var item in tt.JiXiaoNeiRongs)
            {
                var keshistr = _IMoBan.GetMoBanKaoHeFanWei(item.nrid, username);
                if (!string.IsNullOrWhiteSpace(keshistr))
                {
                    List<KeShi> fw = JsonConvert.DeserializeObject<List<KeShi>>(keshistr);
                    item.KaoHeFanWeis = fw;
                }
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult GetAllKeShi()
        {
            //return Json(dynamic, JsonRequestBehavior.AllowGet);
            var str = _IKeShi.GetAllKeShiList();
            List<KeShi> tt = JsonConvert.DeserializeObject<List<KeShi>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取科室内职务
        /// </summary>
        /// <param name="keshiid"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetKeShiZhiWu(int keshiid)
        {
            var lst = _IKeShi.GetKeShiZhiWu(keshiid);
            var datalist = JsonConvert.DeserializeObject<List<ZhiWu>>(lst);
            return Json(datalist, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public void SaveKaoHeZhiWu(int nrid,int zwid,string username)
        {
            _IMoBan.AddKaoHeFanWei(nrid, zwid, username);
        }
        public JsonResult GetGZJHRenYuan(int mbid, string username)
        {
            string str = _IMoBan.GetGZJHInfo(mbid);
            GongZuoJiHua tt = JsonConvert.DeserializeObject<GongZuoJiHua>(str);
            foreach (var item in tt.GongZuoJiHuaNeiRongs)
            {
                var keshistr = _IMoBan.GetMoBanKaoHeFanWeiRenYuan(item.nrid, username);
                if (!string.IsNullOrWhiteSpace(keshistr))
                {
                    List<JiaoZhiGong> fw = JsonConvert.DeserializeObject<List<JiaoZhiGong>>(keshistr);
                    item.RenYuanList = fw;
                }
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public void SaveKaoHeRenYuan(int nrid, string ry, string username)
        {
            _IMoBan.AddKaoHeFanWeiRenYuan(nrid, ry, username);
        }
        [HttpGet]
        public void DelKaoHeFanWei(int nrid, string zwids,string username)
        {
            _IMoBan.DelKaoHeFanWei(nrid, zwids, username);
        }
    }
}